import { Message } from '@/modules/Message.js'
import { reactive } from 'vue'

class DialogueManager {
    constructor() {
        // 使用数组存储对话记录
        this.dialogues = reactive([new Message('assistant','你好！有什么可以帮你的吗？')])
    }

    // 添加消息
    addMessage(role, content, timestamp) {
        const message = new Message()
        message.setRole(role)
        message.setContent(content)
        message.setTimestamp(timestamp)
        this.dialogues.push(message)
        return message
    }

    // 更新最后一条消息
    updateLastMessage(content) {
        // console.log(content)
        if (this.dialogues.length > 0) {
            this.dialogues[this.dialogues.length - 1].content += content
        }
    }

    clearLastMessage() {
        this.dialogues[this.dialogues.length - 1].content = ""
    }

    // 获取所有对话
    getAllDialogues() {
        return this.dialogues
    }

    // 清空对话
    clearDialogues() {
        this.dialogues.splice(0,this.dialogues.length,new Message('assistant','你好！有什么可以帮你的吗？'))
        console.log("cleared",this.dialogues)
    }
}

export const dialogueManager = new DialogueManager() 